// pages/sales/custom_detail/follow.js
var cf = require("../../../config.js");
var util = require("../../../utils/util.js");
var cusmallToken = wx.getStorageSync('cusmallToken');
var baseHandle = require("../../template/baseHandle.js");
//获取应用实例
var app = getApp();
Page(Object.assign({}, baseHandle, {

  /**
   * 页面的初始数据
   */
  data: {
    extConfig: wx.getExtConfigSync ? wx.getExtConfigSync() : {},
    app: app,
    staticResPath: cf.config.staticResPath,
    userImagePath: cf.config.userImagePath,
    followDesc:"",
    mFollowDesc:"",
    sentenceTplShow:false,
    placeholderTxt:"这里输入跟进语",
    isLoading:false,
    textareaShow:true,
    sentence:[
      "客户查看了公司产品，有合作意向",
      "标记一下，客户有合作意向",
      "标记一下，需要给客户发送报价",
      "已加微信好友，后续持续跟进",
      "标记一下，计划近期安排拜访",
      "见面聊过，意向不错",
      "曾拜访过的客户",
      "已发报价，待客户反馈",
      "已成交客户，维护好客户关系",
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu();
    var ctx = this;
    ctx.setData({
      eId: options.eId,
      openId: options.openId
    })
    app.getUserInfo(this, options, function (userInfo, res) {
      cusmallToken = wx.getStorageSync('cusmallToken');
    });
  },
  showSentenceTpl(){
    var ctx = this;
    
    ctx.setData({
      sentenceTplShow: !ctx.data.sentenceTplShow
    });
    if(ctx.data.sentenceTplShow){
      ctx.setData({
        mFollowDesc: ctx.data.followDesc,
        followDesc:"",
        textareaShow:false
      });
    }else{
      // setTimeout(function(){
        
      // },200);
      ctx.setData({
        followDesc: ctx.data.mFollowDesc,
        textareaShow: true,
      });
    }
  },
  selectSen(e){
    var idx = e.currentTarget.dataset.idx;
    var ctx  = this;
    ctx.setData({
      followDesc: ctx.data.sentence[idx],
      sentenceTplShow: false,
      textareaShow: true
    });
  },
  followInput(e){
    var ctx = this;
    ctx.setData({
      followDesc:e.detail.value
    });
  },
  followCommit(e){
    var ctx = this;
    if (0 === ctx.data.followDesc.length){
      wx.showToast({
        title: '跟进描述不能为空',
        icon:"none"
      });
      return;
      // 

    }
    if (ctx.data.isLoading){
      wx.showToast({
        title: '不要重复提交',
        icon: "none"
      });
      return;
    }
    ctx.setData({
      isLoading:true
    });
    wx.showLoading({
      title: '提交中'
    });
    wx.request({
      url: cf.config.pageDomain + "/applet/mobile/visitcard/bahavior/addBehaviorTrail",
      data: {
        cusmallToken: cusmallToken,
        eid: ctx.data.eId,
        openId: ctx.data.openId,
        type: -1,
        sttr2: ctx.data.followDesc,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var data = res.data;
          wx.hideLoading()
        if (data && 0 === data.ret) {
          wx.showToast({
            title: '跟进成功',
            icon: "none",
            success(){
              setTimeout(function(){
                let pages = getCurrentPages();//当前页面
                let prevPage = pages[pages.length - 2];//上一页面
                prevPage.setData({//直接给上移页面赋值
                  isReloadList: true
                });
                wx.navigateBack({
                  delta: 1
                });
              },1000);
            }
          });
        }else{
          wx.showToast({
            title: data.msg,
            icon: "none"
          });
        }
      },
      fail() {
        wx.hideLoading()
      },
      complete() {
        
        ctx.setData({
          isLoading: false
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
}))